
//var ws = io.connect('http://10.8.76.74:9092/');
var ws = io.connect('http://192.168.31.113:9092/');
var $info = document.getElementById("info");

//动作
function doAction(msg) {
    switch (msg) {
        case "up+":
            fireKeyEvent(document, 'keydown', keyboard.W);
            break;
        case "down+":
            fireKeyEvent(document, 'keydown', keyboard.S);
            break;
        case "left+":
            fireKeyEvent(document, 'keydown', keyboard.A);
            break;
        case "right+":
            fireKeyEvent(document, 'keydown', keyboard.D);
            break;
        case "A+":
            fireKeyEvent(document, 'keydown', keyboard.SPACE);
            break;
        case "B+":
            fireKeyEvent(document, 'keydown', keyboard.ENTER);
            break;

        case "up-":
            fireKeyEvent(document, 'keyup', keyboard.W);
            break;
        case "down-":
            fireKeyEvent(document, 'keyup', keyboard.S);
            break;
        case "left-":
            fireKeyEvent(document, 'keyup', keyboard.A);
            break;
        case "right-":
            fireKeyEvent(document, 'keyup', keyboard.D);
            break;
        case "A-":
            fireKeyEvent(document, 'keyup', keyboard.SPACE);
            break;
        case "B-":
            fireKeyEvent(document, 'keyup', keyboard.ENTER);
            break;
    }
}

ws.on('connect', function () {
    console.log("veiw连接成功");
    $info.innerHTML = "连接成功";
    setTimeout(function () {
        $info.style.display = "none";
    }, 2000)

    //标记view页面
    ws.emit('setViewName', true);
})

//接收到服务器发布的信息
ws.on('viewMessage', function (msg) {
    console.log("接收到服务器发布的信息", msg);
    doAction(msg)
})

ws.on('error', function () {
    console.log('ws错误:', ws);
    ws.disconnect();
})

//模拟按键触发
function fireKeyEvent(el, evtType, keyCode){  
    var doc = el.ownerDocument || el,  
        win = doc.defaultView || doc.parentWindow,  
        evtObj;  
    if(doc.createEvent){  
        if(win.KeyEvent) {  
            evtObj = doc.createEvent('KeyEvents');  
            evtObj.initKeyEvent( evtType, true, true, win, false, false, false, false, keyCode, 0 );  
        }  
        else {  
            evtObj = doc.createEvent('UIEvents');  
            Object.defineProperty(evtObj, 'keyCode', {  
                get : function() { return this.keyCodeVal; }  
            });       
            Object.defineProperty(evtObj, 'which', {  
                get : function() { return this.keyCodeVal; }  
            });  
            evtObj.initUIEvent( evtType, true, true, win, 1 );  
            evtObj.keyCodeVal = keyCode;  
            if (evtObj.keyCode !== keyCode) {  
                console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配");  
            }  
        }  
        el.dispatchEvent(evtObj);  
    }   
    else if(doc.createEventObject){  
        evtObj = doc.createEventObject();  
        evtObj.keyCode = keyCode;  
        el.fireEvent('on' + evtType, evtObj);  
    }  
} 